package core.controller;

import core.config.EventDescription;
import core.exceptions.ErrorHandler;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.*;

public abstract class EventHandler {

    protected HttpSession session;
    protected EventDescription eventDescription;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected Event event;
    protected Map<String, String[]> parameters = new HashMap<String, String[]>();
    private List<ErrorHandler> errors = new ArrayList<ErrorHandler>();


    /**
     * Attache to the current instance a sessions
     *
     * @param session to attache
     */
    void setSession(HttpSession session) {
        this.session = session;
    }

    /**
     * Attache to the current instance a request
     *
     * @param request HttpServletRequest
     */
    void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    /**
     * Attache to the current instance a response
     *
     * @param response
     */
    void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * Attache to the current instance a response
     *
     * @param response
     */
    void setEventDescription(EventDescription eventDescription) {
        this.eventDescription = eventDescription;
    }

    /**
     * Sets the set of parameters to the current EventHandler
     *
     * @param parameters parameter map.
     */
    void setParameters(Map<String, String[]> parameters) throws Exception {
        this.parameters = parameters;
        this.event = new Event(parameters);
    }

    /**
     * Returns the next URL based on a ResponseKey
     *
     * @param responseKey String response key to be located in
     * eventmapptings.xml
     * @return the URL to where the request will be redirect;
     */
    public String getNextURL(String responseKey) {
        return eventDescription.getResponse(responseKey).getValue();
    }

    /**
     * Append an error to the existing error list.
     *
     * @param errors
     */
    protected void addError(ErrorHandler error) {
        this.errors.add(error);
    }

    /**
     * Appends a list of error to this object.
     *
     * @param errors
     */
    protected void addErrors(List<ErrorHandler> errors) {
        this.errors.addAll(errors);
    }

    /**
     * Sets the list of errors;
     *
     * @param errors the list of errors
     */
    protected void setErrors(List<ErrorHandler> errors) {
        this.errors = errors;
    }

    protected boolean hasErrors() {
        return this.errors.size() > 0;
    }

    /**
     * Returns the error list;
     *
     * @return list of ErrorHandler
     */
    List<ErrorHandler> getErrors() {
        return errors;
    }

    /**
     * Describes the event actions
     *
     * @return the string key for the page return, returns null if no
     * redirection is needed
     */
    public abstract String perform();
}
